# import base64
# from cryptography.hazmat.primitives import padding
# from cryptography.hazmat.primitives.ciphers import algorithms
# from Crypto.Cipher import AES
# from binascii import b2a_base64, a2b_base64
#
#
# class PrpCrypt(object):
#
#     def __init__(self, key, iv):
#         self.key = key.encode('utf-8')
#         self.iv = iv.encode('utf-8')
#         self.mode = AES.MODE_CBC
#
#     # 加密函数，如果text不足16位就用空格补足为16位，
#     # 如果大于16当时不是16的倍数，那就补足为16的倍数。
#     def encrypt(self, text):
#         cryptor = AES.new(self.key, self.mode, self.iv)
#         text = text.encode('utf-8')
#
#         # 这里密钥key 长度必须为16（AES-128）,
#         # 24（AES-192）,或者32 （AES-256）Bytes 长度
#         # 目前AES-128 足够目前使用
#
#         text = self.pkcs7_padding(text)
#
#         self.ciphertext = cryptor.encrypt(text)
#
#         # 因为AES加密时候得到的字符串不一定是ascii字符集的，输出到终端或者保存时候可能存在问题
#         # 所以这里统一把加密后的字符串转化为16进制字符串
#         return b2a_base64(self.ciphertext)
#
#     @staticmethod
#     def pkcs7_padding(data):
#         if not isinstance(data, bytes):
#             data = data.encode()
#
#         padder = padding.PKCS7(algorithms.AES.block_size).padder()
#
#         padded_data = padder.update(data) + padder.finalize()
#
#         return padded_data
#
#     @staticmethod
#     def pkcs7_unpadding(padded_data):
#         unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
#         data = unpadder.update(padded_data)
#
#         try:
#             uppadded_data = data + unpadder.finalize()
#         except ValueError:
#             raise Exception('无效的加密信息!')
#         else:
#             return uppadded_data
#
#
#     # 解密后，去掉补足的空格用strip() 去掉
#     def decrypt(self, text):
#         # 偏移量'abcdefg'
#         cryptor = AES.new(self.key, self.mode, self.iv)
#         plain_text = cryptor.decrypt(a2b_base64(text))
#         # return plain_text.rstrip('\0')
#         return bytes.decode(plain_text).rstrip('\0')
#
#
# # 加解密
# if __name__ == '__main__':
#     # pc = PrpCrypt('G862bPOJA4r02K4YN2YxBXghyyKA6Wv6', '8mrPxVOFQR5KZ5hO')  # 初始化密钥
#     # e = pc.encrypt('{"LoginName":"342623196610030018","Password":"nh920815","ValidateCode":"xmax","InterfaceName":"Account/SignIn"}')  # 加密
#     # d = pc.decrypt(e)  # 解密
#     # print("加密:", e.decode())
#     # print("解密:", d)
#     pc = PrpCrypt('RnJ36s2bMlww5L3ZjvrkyQDIrEnqZxTq', 'Mj6TzyOAh3zzUHgC')  # 初始化密钥
#     e = pc.encrypt('{"ProductUnityCode":"1000172120","CurPage":1,"PageSize":10,"InterfaceName":"GenlOrder/GetProTradingRecord"}')  # 加密
#     d = pc.decrypt(e)  # 解密
#     print("加密:", e.decode())
#     print("解密:", d)
